<?php

include_once($_SERVER["DOCUMENT_ROOT"]. "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/user/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/mblog/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/album/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/mblog/inc.php");
$g_cgival = array();
$g_show = array();
$g_pro = array();


if(! security_check::check_refer())
{
	throw new Exception("来路不明");
}

function check_cgi_pro()
{
	global $g_cgival, $g_show, $g_pro;
	cgi::both($g_cgival['t'],"t","json");
	$g_cgival['t'] = string::un_html($g_cgival['t']);

	cgi::both($g_cgival['callback'], "callback", "");
	$g_cgival['callback'] = string::un_html(string::un_script_code($g_cgival['callback']));
	
	
	cgi::both($g_cgival['content'],"content","");
	$g_cgival['content'] = string::un_html($g_cgival['content']);
	
	cgi::both($g_cgival['uid'],"uid","");
	$g_cgival['uid'] = intval($g_cgival['uid']);
	
	cgi::both($g_cgival['pid'],"pid","");
	$g_cgival['pid'] = string::un_html($g_cgival['pid']);
	
	cgi::both($g_cgival['p_width'],"p_width","");
	$g_cgival['pid'] = intval($g_cgival['p_width']);
	
	cgi::both($g_cgival['p_height'],"p_height","");
	$g_cgival['p_height'] = intval($g_cgival['p_height']);
	
	cgi::both($g_cgival['album_id'],"album_id",0);
	
	
}


function get_data()
{
	global $g_cgival, $g_show, $g_pro;
	
	$pic = $g_cgival['pic'];
	$content = $g_cgival['content'];
	$pid = $g_cgival['pid'];
	$album_id = $g_cgival['album_id'];
	$user_info = new user_info();
	$is_login = $user_info->is_logined();
	
	if($is_login)
	{
		$user_detail_info = $user_info->get_cur_user_info();
		$current_uid = $user_detail_info["uid"];
		
	}
	else
	{
		throw new Exception("未登录！");
	}
	
	
	$uid = $g_cgival['uid'];
	
	if($current_uid == $uid)
	{
		
		throw new Exception("自己都不认识了?");
	}
	
	
	$db_user_message = new db_user_message();
	$last_time = date("Y-m-d H:i:s",time()-60);
	$get_count = $db_user_message->get_user_have_send_count($current_uid,$last_time);
	if($get_count > 10)
	{
		throw new Exception("您说的太快了!");
	}
	else
	{
		$last_time = date("Y-m-d H:i:s",time()-3600);
		$get_count = $db_user_message->get_user_have_send_count($current_uid,$last_time);
		
		if($get_count > 100)
		{
			throw new Exception("您说的太快了!");
		}
	}
	
	
	//update老数据
	$db_user_message->update_last_by_from_uid_and_to_uid($current_uid,$uid);
	
	
	
	
	//weibo同步
	mod_mblog::update_private_message_by_uid($uid);//同步微博的私信过来
	//发的私信同步到微博去
	$db_user_token = new db_user_token();
	$token_info  = $db_user_token->get_token_by_uid_and_appkey($current_uid,WB_AKEY);
	$token = $token_info["access_token"];
	$mod_get_mblog_from_json = new mod_get_mblog_from_json();
	$res = $mod_get_mblog_from_json->api_add_private_message(WB_AKEY,$token,$uid,$content);
	
	
	//插入数据
	$res = $db_user_message->insert_user_message($current_uid,$uid,$content,$res["id"]);
	
	if(!$res)
	{
		throw new Exception("您说的太快了!");
	}
	$g_show["res"] = $res;
}

 
try
{
	check_cgi_pro();
	get_data();

	$g_show["error"] = "0";
	$g_show["errmsg"] = "发布成功～";
}
catch (Exception $e)
{
	$g_show["error"] = "1";
	$g_show["errmsg"] = $e->getMessage();
}
show_pro();
exit();


function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $config;
	
	if ($g_cgival['t'] == "serialize")
	{
		echo serialize($g_show);
	}
	elseif ($g_cgival['t'] == "json")
	{
		echo output::json("json", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "jsonp")
	{
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "perl")
	{
		$string = php_to_perl::php_array_to_perl_hash($g_show);
		$string = mb_convert_encoding($string, "GBK", "UTF-8");
		echo $string;
	}
	elseif($g_cgival['t'] == "iframe")
	{
		header("location:{$g_cgival['link']}");
	}
	else
	{
		
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
}